package com.zxh.controller;

import com.zxh.pojo.User;
import com.zxh.service.UserService;
import com.zxh.utils.FileUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/user")
@Controller
public class UserContorller {

    @Resource
    private UserService userService;


    @ResponseBody
    @RequestMapping("/updateUser")
    public Object updateUser(User user, MultipartFile headImg, HttpSession session) throws IOException {
        //上传文件，获取存储的文件名
        String fileName = FileUtil.saveFile(session, headImg);

        //将文件名存到对象中
        user.setHeadImgName(fileName);

        //修改用户数据
        int i = userService.updateUser(user);
        System.out.println(i+"------------------------------------------------------------------------------------------");
        return i;
    }

    /**
     * 修改用户根据usercode获取用户数量
     * 排除当前用户
     * @param usercode
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("/checkUserCodeOfId/{usercode}/{id}")
    public Object checkUserCodeOfId(@PathVariable String usercode,@PathVariable Integer id){
        int count = userService.checkUserOfId(usercode,id);
        return count;
    }
    /**
     * 根据usercode获取用户数量
     * @param usercode
     * @return
     */
    @ResponseBody
    @RequestMapping("/checkUsercode/{usercode}")
    public Object checkUserCode(@PathVariable String usercode){
        int count = userService.checkUserCountByCode(usercode);
        return count;
    }

    /**
     * 根据ID删除用户
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("/delUserById/{id}")
    public Object delUserById(@PathVariable Integer id){
        int count = userService.delUserById(id);
        return count;
    }

    /**
     * 根据ID获取用户详情
     */
    @ResponseBody
    @RequestMapping("/getUserById/{id}")
    public Object getUserById(@PathVariable Integer id) throws IOException {
        return userService.getUserById(id);
    }

    /**
     * 添加用户，并且上传头像
     * @param user
     * @param headImg
     * @param session
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping("/addUser")
    public Object addUser(User user, MultipartFile headImg, HttpSession session) throws IOException {
        //上传文件，获取存储的文件名
        String fileName = FileUtil.saveFile(session, headImg);

        //将文件名存到对象中
        user.setHeadImgName(fileName);

        //将用户数据添加到数据库
        return userService.addUser(user);
    }

    /**
     * 查询用户列表-根据指定参数，分页
     * @param userName 用户名
     * @param userRole 用户角色id
     * @param pageNow 页码
     * @param pageSize 每页行数
     */
    @ResponseBody
    @RequestMapping("/getUserListByParams")
    public Object getUserListByParams(String userName, Integer userRole,
                                    @RequestParam Integer pageNow, @RequestParam Integer pageSize) throws IOException {

        //查询用户总数-根据条件
        int total = userService.getUserCountByParams(userName, userRole);

        //查询用户列表-根据条件，分页
        Map<String,Object> params = new HashMap<>();
        params.put("userName", userName);
        params.put("userRole", userRole);
        params.put("start", (pageNow-1)*pageSize); //计算limit起始行索引
        params.put("pageSize", pageSize);
        List<User> userList = userService.getUserListByParams(params);

        //封装数据，响应给客户端
        Map<String, Object> result = new HashMap<>();
        result.put("total",total);//用户总数
        result.put("userList",userList);//用户列表

        return result;
    }

    /**
     * 退出登录
     * @param session
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping("/logout")
    public Object logout(HttpSession session) throws IOException {
        //使当前会话session失效
        session.invalidate();

        return true;
    }

    /**
     * 用户登录接口
     *
     *
     *
     * param userCode
     * @param userPassword
     */
    @ResponseBody
    @RequestMapping(value="/getUserByLogin", method = RequestMethod.POST)
    public Object getUserByLogin(@RequestParam String userCode, @RequestParam String userPassword,
                               HttpSession session) throws IOException {
        //根据登录信息查询用户对象
        User user = userService.getUserByLogin(userCode,userPassword);

        if(user != null){
            //将登录成功的用户对象，存储到会话对象
            session.setAttribute("LOGIN_KEY",user);
        }

        return user;
    }

}
